package com.gateway.security.authentication.client;

import com.gateway.common.enums.AuthenticationTypeEnum;
import com.gateway.plugin.security.oauth2.client.OAuth2ClientPrincipalConverter;
import com.gateway.security.common.authentication.client.ClientPrincipal;
import com.gateway.security.common.authentication.client.ClientPrincipalConverter;
import com.gateway.security.common.authentication.client.DefaultClientPrincipalConverter;
import com.gateway.plugin.security.uaa.authentication.client.BasicClientPrincipalConverter;
import com.gateway.plugin.security.uaa.authentication.client.MobileApplicationClientPrincipalConverter;
import com.google.common.collect.Maps;
import reactor.core.publisher.Mono;

import java.util.Map;

/**
 * @program: api-gateway
 * @description:
 * @author: YuKai Fan
 * @create: 2025/2/12 21:18
 **/
public class ClientPrincipalConverterFactory {
    private static final Map<String, ClientPrincipalConverter> converterDataMap;

    static {
        converterDataMap = Maps.newHashMapWithExpectedSize(AuthenticationTypeEnum.values().length);
        converterDataMap.put(AuthenticationTypeEnum.BASIC.getType(), new BasicClientPrincipalConverter());
        converterDataMap.put(AuthenticationTypeEnum.MOBILE_APPLICATION.getType(), new MobileApplicationClientPrincipalConverter());
        converterDataMap.put(AuthenticationTypeEnum.OAUTH2.getType(), new OAuth2ClientPrincipalConverter());
    }

    private ClientPrincipalConverterFactory() {
    }

    /**
     * New instance ClientPrincipalConverter
     *
     * @param type the Authentication type
     * @return the ClientPrincipalConverter
     */
    public static ClientPrincipalConverter newInstance(final String type) {
        return converterDataMap.getOrDefault(type, DefaultClientPrincipalConverter.getInstance());
    }

    public static Mono<ClientPrincipal> convert(String type, Map<String, Object> claims) {
        return newInstance(type).convert(claims);
    }
}
